package com.thti.mecool.client.adapter;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.thti.mecool.client.activity.R;
import com.thti.mecool.client.model.po.ProjectServer;

public class MakePlanSelectTaskAdapter extends BaseAdapter {
	private Context mContext;
    List<ProjectServer> projectServerList;
    
	public MakePlanSelectTaskAdapter(Context mContext,
			List<ProjectServer> projectServerList) {
		super();
		this.mContext = mContext;
		this.projectServerList = projectServerList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return projectServerList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return projectServerList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view =((Activity) mContext).getLayoutInflater().inflate(R.layout.mecool_make_plan_select_task_item,null);
		ImageView importmntImageView;
		TextView textView,textnumber;
		try {
			importmntImageView=(ImageView)view.findViewById(R.id.mecool_make_plan_select_task_item_important);
			textnumber=(TextView)view.findViewById(R.id.mecool_make_plan_select_task_item_shownum);
			view.setTag(position);
			textView=(TextView)view.findViewById(R.id.mecool_make_plan_select_task_item_content);
			textView.setText(projectServerList.get(position).getName());
			if(projectServerList.get(position).getCriticalJob()==1){
				importmntImageView.setVisibility(View.VISIBLE);
			}else if(projectServerList.get(position).getCriticalJob()==0){
				importmntImageView.setVisibility(View.INVISIBLE);
			}
			if(projectServerList.get(position).getRequiredCount()!=-1&&projectServerList.get(position).getFinishedCount()!=-1){
				textnumber.setVisibility(View.VISIBLE);
				if(projectServerList.get(position).getRequiredCount()>projectServerList.get(position).getFinishedCount()){
					textnumber.setText(String.valueOf((projectServerList.get(position).getRequiredCount()-projectServerList.get(position).getFinishedCount())));
				}else{
					textnumber.setText("0");
				}
				
			}else{
				textnumber.setVisibility(View.INVISIBLE);
			}
			textnumber.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					dialog(mContext,projectServerList.get(position).getFinishedCount(),projectServerList.get(position).getRequiredCount());
				}
			});
		
		} catch (Exception e) { 
			// TODO: handle exception
		}
		return view;
	}

	public void dialog(Context mContext,int finishedCount,int requiredCount) {
		View showTimes=((Activity) mContext).getLayoutInflater().inflate(R.layout.mecool_make_plan_select_task_item_bubble_dialog,null);
		TextView finishedCountTextView=(TextView)showTimes.findViewById(R.id.mecool_make_plan_select_task_item_bubble_dialog_donenum);
		TextView unFinishedCountTextView=(TextView)showTimes.findViewById(R.id.mecool_make_plan_select_task_item_bubble_dialog_undonenum);
		finishedCountTextView.setText(String.valueOf(finishedCount));
		if(requiredCount>finishedCount){
			unFinishedCountTextView.setText(String.valueOf((requiredCount-finishedCount)));
		}else{
			unFinishedCountTextView.setText("0");
		}
		
		AlertDialog.Builder builder = new Builder(mContext);
		builder.setView(showTimes);
		builder.setTitle("点击提醒");
		builder.setPositiveButton("我知道了",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();

					}
		});
		builder.create().show();
	}
}
